package com.dongjiantong.openplatform.entity;

/**
 * @author cao
 * @create 2017-12-13 下午7:37
 **/
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.google.common.base.MoreObjects;
import java.io.Serializable;

@JsonInclude(Include.NON_NULL)
public class OPResponse<T> implements Serializable {
    private static final long serialVersionUID = -8917056638184827729L;
    private boolean success;
    private T result;
    private String error;
    private String errorMessage;

    public OPResponse() {
    }

    public void setResult(T result) {
        this.success = true;
        this.result = result;
    }

    public void setError(String error) {
        this.success = false;
        this.error = error;
    }

    private void setError(String error, String errorMessage) {
        this.success = false;
        this.error = error;
        this.errorMessage = errorMessage;
    }

    public static <T> OPResponse<T> ok(T data) {
        OPResponse<T> resp = new OPResponse();
        resp.setResult(data);
        return resp;
    }

    public static <T> OPResponse<T> ok() {
        return ok(null);
    }

    public static <T> OPResponse<T> fail(String error) {
        OPResponse<T> resp = new OPResponse();
        resp.setError(error);
        return resp;
    }

    public static <T> OPResponse<T> fail(String error, String errorMessage) {
        OPResponse<T> resp = new OPResponse();
        resp.setError(error, errorMessage);
        return resp;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this).add("success", this.success).add("result", this.result).add("error", this.error).add("errorMessage", this.errorMessage).omitNullValues().toString();
    }

    public boolean isSuccess() {
        return this.success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getResult() {
        return this.result;
    }

    public String getError() {
        return this.error;
    }

    public String getErrorMessage() {
        return this.errorMessage;
    }
}
